{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "l1 = [20, 8, 19, 10, 13, 27, 23, 9, 58, 45, 34, 18, 17, 27, 33, 58, 27, 51, 85, 28, 18, 38, 34, 67, 18, 7, 32, 38, 63, 38, 7, 3, 52, 21, 27, 22, 39, 29, 37, 22, 86, 51, 62, 22, 57, 60, 54, 62, 52, 34, 37, 21, 60, 64, 102, 101, 60, 56, 66, 20, 36, 55, 33, 40, 44, 24, 40, 20, 40, 35, 35, 23, 93, 50, 62, 31, 54, 53, 81, 44, 80, 67, 100, 22, 78, 56, 58, 50, 43, 14, 42, 17, 40, 53, 59, 54, 12, 5, 28, 40, 63, 12, 13, 13, 42, 21, 21, 24, 36, 39, 47, 33, 66, 23, 21, 18, 25, 31, 51, 73, 25, 14, 25, 20, 33, 26, 18, 15]\n",
    "l2 = [40,25,11,0,22,31,6,8,10,3,0,1,30,91,88,18,38,44,16,1,5,25,70,36,1,22,10,7,10,40,61,1,60,9,8,4,111,45,21,45,9,1,1,14,111,111,70,26,95,13,3,2,3,39,111,111,20,3,11,11,1,32,70,22,48,8,9,25,60,26,14,37,4,5,65,110,111,31,1,0,101,78,84,34,4,2,2,29,33,44,25,22,2,0,4,18,54,51,24,21,12,18,5,6,11,17,100,65,50,92,37,14,23,77,95,9,3,14,60,40,4,30,23,32,10,3,19,13]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "import math\n",
    "def L2(l1, l2):\n",
    "    distance = 0\n",
    "    \n",
    "    for i in range(len(l1)):\n",
    "        diff = l2[i]-l1[i]\n",
    "        distance += diff*diff\n",
    "\n",
    "    return math.sqrt(distance)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "404.74683445334074"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "L2(l1, l2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0x13C80\n"
     ]
    }
   ],
   "source": [
    "#计算某个vid的地址\n",
    "vid = 91\n",
    "vector_per_block = 23\n",
    "vector_size = 704\n",
    "PAGE_SIZE = 16384\n",
    "\n",
    "blocks = 1+vid//23 #属于第几个块 \n",
    "addr = blocks*PAGE_SIZE + (vid%23)*vector_size\n",
    "print(\"0x%X\"%addr)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "cache size 190496768 B, 186032.0 KB, 181.671875 MB\n"
     ]
    }
   ],
   "source": [
    "#计算缓存所需的空间\n",
    "clusters = 256\n",
    "bytes_per_node = 704\n",
    "neighbor_count = 32\n",
    "hop = 3 #缓存几跳\n",
    "cache_node_per_cluster = 0\n",
    "for i in range(hop):\n",
    "    cache_node_per_cluster += neighbor_count**i\n",
    "    # print(\"cached\",cache_node_per_cluster,\"node\")\n",
    "\n",
    "cache_size = clusters * cache_node_per_cluster * bytes_per_node\n",
    "print(\"cache size\",cache_size,\"B,\",cache_size/1024,\"KB,\",cache_size/(1024*1024),\"MB\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "base",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
